<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  

  
  <title>Using Grep &amp; Regular Expressions | ZHB&#39;s Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="From Using Grep &amp;amp; Regular Expressions to Search for Text Patterns in Linux">
<meta name="keywords" content="Command Line,Linux,Regular Expression">
<meta property="og:type" content="article">
<meta property="og:title" content="Using Grep &amp; Regular Expressions">
<meta property="og:url" content="https://gkzhb.github.io/2018/11/grep/index.html">
<meta property="og:site_name" content="ZHB&#39;s Blog">
<meta property="og:description" content="From Using Grep &amp;amp; Regular Expressions to Search for Text Patterns in Linux">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-12-14T11:36:45.323Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Using Grep &amp; Regular Expressions">
<meta name="twitter:description" content="From Using Grep &amp;amp; Regular Expressions to Search for Text Patterns in Linux">
  
    <link rel="alternate" href="/atom.xml" title="ZHB&#39;s Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link rel="stylesheet" href="/css/sourcecodepro.css">
    <!-- <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css"> -->
  
  <link rel="stylesheet" href="/css/style.css">
</head>
</html>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">ZHB&#39;s Blog</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gkzhb.github.io"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-grep" class="article article-type-post" itemscope itemprop="blogPost">



  <div class="article-meta">
    <a href="/2018/11/grep/" class="article-date">
  <time datetime="2018-11-07T09:33:47.000Z" itemprop="datePublished">2018-11-07</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Linux/">Linux</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Using Grep &amp; Regular Expressions
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
		<!-- Table of Contents -->
		
			<div id="toc" class="toc-article">
				<strong class="toc-title">Table of Content</strong>
				<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#basic-usage"><span class="toc-number">1.</span> <span class="toc-text">Basic Usage</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#a-little-bit-about-regular-expressions"><span class="toc-number">2.</span> <span class="toc-text">A Little bit about Regular Expressions</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#anchor-matches"><span class="toc-number">2.1.</span> <span class="toc-text">Anchor Matches</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#matching-any-character"><span class="toc-number">2.2.</span> <span class="toc-text">Matching Any Character</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#bracket-expressions"><span class="toc-number">2.3.</span> <span class="toc-text">Bracket Expressions</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#repeat-pattern-zero-or-more-times"><span class="toc-number">2.4.</span> <span class="toc-text">Repeat Pattern Zero or More Times</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#escaping-meta-characters"><span class="toc-number">2.5.</span> <span class="toc-text">Escaping Meta-Characters</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#extended-regular-expressions"><span class="toc-number">3.</span> <span class="toc-text">Extended Regular Expressions</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#groupinn"><span class="toc-number">3.1.</span> <span class="toc-text">Groupinn</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#alternation"><span class="toc-number">3.2.</span> <span class="toc-text">Alternation</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#quantifiers"><span class="toc-number">3.3.</span> <span class="toc-text">Quantifiers</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#specifying-match-repetition"><span class="toc-number">3.4.</span> <span class="toc-text">Specifying Match Repetition</span></a></li></ol></li></ol>
			</div>
		
        <p>From <a href="https://www.digitalocean.com/community/tutorials/using-grep-regular-expressions-to-search-for-text-patterns-in-linux" target="_blank" rel="noopener">Using Grep &amp; Regular Expressions to Search for Text Patterns in Linux</a></p>
<a id="more"></a>
<h2 id="basic-usage">Basic Usage</h2>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ grep [Pattern] [Input File]</span><br></pre></td></tr></table></figure>
<p>Print out every <strong>line</strong> in the file containing that Pattern</p>
<table>
<thead>
<tr class="header">
<th>Common Options</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>-i</code> == <code>--ignore-case</code></td>
<td>both upper- and lower-case variations</td>
</tr>
<tr class="even">
<td><code>-v</code> == <code>--invert-match</code></td>
<td>do not contain the Pattern</td>
</tr>
<tr class="odd">
<td><code>-n</code> == <code>--line-number</code></td>
<td>show the line number</td>
</tr>
</tbody>
</table>
<h2 id="a-little-bit-about-regular-expressions">A Little bit about Regular Expressions</h2>
<h3 id="anchor-matches">Anchor Matches</h3>
<table>
<thead>
<tr class="header">
<th style="text-align: center;">Anchor Matches</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;"><code>^</code></td>
<td>at the begining of the string(line)</td>
</tr>
<tr class="even">
<td style="text-align: center;"><code>$</code></td>
<td>at the end of the string</td>
</tr>
</tbody>
</table>
<h3 id="matching-any-character">Matching Any Character</h3>
<table>
<thead>
<tr class="header">
<th style="text-align: center;">Any Character</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;"><code>.</code></td>
<td>match any single character</td>
</tr>
</tbody>
</table>
<h3 id="bracket-expressions">Bracket Expressions</h3>
<ul>
<li>Characters between <code>[</code> and <code>]</code> means <code>OR</code></li>
<li>But if the characters start with <code>^</code>, it means <code>NOT</code>, i.e. <strong>except</strong></li>
<li>Represent a range of characters: <code>[A-Z]</code>, but we can use POSIX character classes to replace the <code>[A-Z]</code>: <code>[[:upper:]]</code></li>
</ul>
<h3 id="repeat-pattern-zero-or-more-times">Repeat Pattern Zero or More Times</h3>
<table>
<thead>
<tr class="header">
<th style="text-align: center;">Character</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;"><code>*</code></td>
<td>repeat the previous character or expression zero or more times</td>
</tr>
</tbody>
</table>
<h3 id="escaping-meta-characters">Escaping Meta-Characters</h3>
<p>Using backslash character <code>\</code> to escape characters that would normally have a special meaning.</p>
<h2 id="extended-regular-expressions">Extended Regular Expressions</h2>
<p>Use <code>-E</code> flag or call <code>egrep</code> command to use Extended Regular Expressions</p>
<h3 id="groupinn">Groupinn</h3>
<h3 id="alternation">Alternation</h3>
<h3 id="quantifiers">Quantifiers</h3>
<table>
<thead>
<tr class="header">
<th style="text-align: center;">Character</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;"><code>?</code></td>
<td>match the previouscharacter zero or one times</td>
</tr>
<tr class="even">
<td style="text-align: center;"><code>+</code></td>
<td>one or more times</td>
</tr>
</tbody>
</table>
<h3 id="specifying-match-repetition">Specifying Match Repetition</h3>
<p><code>{NUM}</code> to specify matching times, from an exact number, a range, or an upper or lower bounds</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gkzhb.github.io/2018/11/grep/" data-id="cjx8qks8d0000nrw8xe9bz2eg" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Command-Line/">Command Line</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Linux/">Linux</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Regular-Expression/">Regular Expression</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2018/12/memory/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          改革开放 40 年回忆
        
      </div>
    </a>
  
  
    <a href="/2018/11/sort/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">排序</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Landscape/">Landscape</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Linux/">Linux</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Programming/">Programming</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Self-hosting/">Self-hosting</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Software/">Software</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Anki/">Anki</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Blog/">Blog</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Command-Line/">Command Line</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Data-Structure/">Data Structure</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Games/">Games</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hometown/">Hometown</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/">Linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Network/">Network</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Read-It-Later/">Read-It-Later</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Recall/">Recall</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Regular-Expression/">Regular Expression</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Server/">Server</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Spring/">Spring</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/折腾/">折腾</a></li></ul>
    </div>
  </div>


  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2019 Haibin Zhang<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<!-- <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> -->

<!-- <script src="http://apps.bdimg.com/libs/jquery/2.0.3/jquery.min.js"></script> -->
<script src="/mathjax/MathJax.js?config=TeX-MML-AM_CHTML"></script>
<script src="/js/jquery.min.js"></script>

  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>




  </div>
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
		messageStyle: "none",
		showProcessingMessages: false,
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<!-- <script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> -->
<script src="/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

</body>
</html>